;; emacs server settings

(server-start)
;;(add-hook 'after-init-hook 'server-start)

;; set load-path to site-lisp
(setq load-path (cons "~/emacs.d/site-lisp" load-path))
(setq load-path (cons "~/emacs.d/site-lisp/tuareg-mode-1.45.6" load-path))


(defun unfill-paragraph ()
  (interactive)
  (let ((fill-column (point-max)))
  (fill-paragraph nil)))

(global-set-key "\M-Q" 'unfill-paragraph)

;; ========== completion ==========

(require 'completion)

;; find-files

(require 'open-resource)
(global-set-key [?\A-r] 'open-resource)
(global-set-key "\C-\M-r" 'open-resource)

(setq open-resource-repository-directory "~/Projects")
(setq open-resource-ignore-patterns (quote ("/target/" "~$" ".old$")))

;; perforce integration
;; (load-library "p4")

;; scheme settings

(setq scheme-program-name "mzscheme")
(add-hook 'scheme-mode-hook
	  (function (lambda ()
		      (local-set-key [f1] 'info-lookup-symbol))))

;; general settings

(auto-fill-mode)

(add-hook 'text-mode-hook
	  (function (lambda ()
		      (local-set-key "\C-c\ w" 'ispell-word))))
		      

(let ((c-stuff (function (lambda ()
                           (setq indent-tabs-mode nil)
                           (c-set-style "stroustrup")
			   (setq c-hungry-delete-key t)))))
  (setq c-mode-hook c-stuff)
  (setq c++-mode-hook c-stuff)
  (setq java-mode-hook c-stuff)
  (setq csharp-mode-hook c-stuff))

(set-register ?e '(file . "~/emacs.d/emacs.el"))

(setq make-backup-files nil) ; stop it from making backups

(defun my-text-mode-settings ()
  (setq mode-require-final-newline nil))

(add-hook 'text-mode-hook 'my-text-mode-settings)

;; disable welcome message
(setq inhibit-startup-message t)
(setq inhibit-startup-echo-area-message t)
(setq initial-scratch-message nil) 

;; editing improvements

(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-k" 'kill-region)
(global-set-key "\C-c\C-k" 'kill-region)

(global-set-key [f5] 'kmacro-end-and-call-macro)

(global-set-key (kbd "S-C-<left>") 'shrink-window-horizontally)
(global-set-key (kbd "S-C-<right>") 'enlarge-window-horizontally)
(global-set-key (kbd "S-C-<down>") 'shrink-window)
(global-set-key (kbd "S-C-<up>") 'enlarge-window)

;; textile (temporary)

(require 'textile-minor-mode)
(setq default-major-mode 'text-mode)

(setq auto-mode-alist
      (append '(("\\.wiki" . textile-minor-mode) 
		("\\.textile" . textile-minor-mode)) auto-mode-alist))


;; regex ide

(require 'regex-tool)

;; yasnippet

(require 'yasnippet)
(yas/initialize)
(yas/load-directory "~/emacs.d/site-lisp/snippets")

;; html

(setq auto-mode-alist
      (append '(("\\.ftl" . html-mode)) auto-mode-alist))		      

;; flymake

(require 'flymake)

(put 'downcase-region 'disabled nil)

;; javascript
(autoload 'js2-mode "js2-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))


;; recent files 
(require 'recentf)
(recentf-mode 1)
(setq recentf-max-menu-items 25)
(global-set-key "\C-x\ \C-r" 'recentf-open-files)

;; c# mode
(autoload 'csharp-mode "csharp-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.cs$" . csharp-mode))

;; template toolkit
(autoload 'template-toolkit-mode "template-toolkit-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.tt$" . template-toolkit-mode))

;; dokywiki
(autoload 'dokuwiki-mode "dokuwiki-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.wiki$" . dokuwiki-mode))

;; color mode
(require 'color-theme)
(color-theme-dark-laptop)

;; open console

(defun open-console ()
  (interactive "*")
  (delete-other-windows)
  (split-window-vertically -20)
  (other-window 1)
  (shell))

(global-set-key [(meta ?s)] 'open-console)

;; clojure mode
(require 'clojure-auto)

;; python settings
(add-hook 'python-mode-hook 
      (lambda ()
	(local-set-key [(control ?<)] 'python-shift-left)
	(local-set-key [(control ?>)] 'python-shift-right)))

;; django stuff
(load "django-html-mode")

;; ruby
(require 'ruby-mode)

;; perl stuff
(global-set-key "\M-p" 'cperl-perldoc-at-point)

;; Use cperl-mode instead of the default perl-mode
;(add-to-list 'auto-mode-alist '("\\.\\([pP][Llm]\\|al\\)\\'" . cperl-mode))
;(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
;(add-to-list 'interpreter-mode-alist '("perl5" . cperl-mode))
;(add-to-list 'interpreter-mode-alist '("miniperl" . cperl-mode))

(add-hook 'cperl-mode-hook 'n-cperl-mode-hook t)
(defun n-cperl-mode-hook ()
  (setq cperl-indent-level 4)
  (setq cperl-continued-statement-offset 0)
  (setq cperl-extra-newline-before-brace t)
  (set-face-background 'cperl-array-face "wheat")
  (set-face-background 'cperl-hash-face "wheat")
  )

;; ocaml

(load "tuareg-mode-1.45.6/append-tuareg.el")

;; increment number

(defun increment-number-at-point ()
  (interactive)
  (skip-chars-backward "0123456789")
  (or (looking-at "[0123456789]+")
      (error "No number at point"))
  (replace-match (number-to-string 
		  (1+ (string-to-number (match-string 0))))))

(global-set-key (kbd "C-c +") 'increment-number-at-point)

;; settings for graphical mode
(tool-bar-mode nil)
;; (color-theme-dark-laptop)
(setq x-select-enable-clipboard t)


(require 'yaml-mode)
(add-to-list 'auto-mode-alist '("\\.yml$" . yaml-mode))


(when (load "flymake" t)
  (defun flymake-pylint-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
		       'flymake-create-temp-inplace))
           (local-file (file-relative-name
                        temp-file
                        (file-name-directory buffer-file-name))))
      (list "epylint" (list local-file))))
  
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pylint-init)))
